//
//  ViewController.m
//  shiyan
//
//  Created by MAC on 15-11-24.
//  Copyright (c) 2015年 me. All rights reserved.
//

#import "ViewController.h"
#import "InputViewController.h"
@interface ViewController ()<InputFinshDelegate>
{
    NSArray * data;
    NSArray * tel;
    NSIndexPath * ZindexPath;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"标题栏";
//    data = @[
//             @"李盼",
//             @"夏志龙",
//             @"杨开宇"
//             ];
//    tel = @[
//            @"159",
//            @"147",
//            @"258"
//            ];
    
    //右上角按钮(编辑)
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.dataSource = self;
//    self.tableView.delegate = self;
    
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
//    [def setObject:data forKey:@"data"];
//    [def setObject:tel forKey:@"tel"];
//    [def synchronize];
    
    data = [def objectForKey:@"data"];
    tel = [def objectForKey:@"tel"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return [data[section]count ];
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //调整字体的位置
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = data[indexPath.row];
    cell.detailTextLabel.text = tel[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //赋值
    ZindexPath = indexPath;
    InputViewController * con = [[InputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    con.tels = cell.detailTextLabel.text;
    
    con.tels = tel[indexPath.row];
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];
    
}

- (void)inputFinished:(NSString *)text
{
    NSMutableArray * arr = tel.mutableCopy;
    [arr replaceObjectAtIndex:ZindexPath.row withObject:text];
    tel = arr;
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
    [def setObject:tel forKey:@"tel"];
    [self.tableView reloadData];
    
}

//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray * arr = data.mutableCopy;
        
        NSMutableArray * inArr = [data[indexPath.section]mutableCopy];
        
        [inArr removeObjectAtIndex:indexPath.row];
        
        [arr replaceObjectAtIndex:indexPath.section withObject:inArr];
        
        data = arr;
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
